//@author A0121492M
public class DeleteCommand extends Command {
	public int taskId = Task.EMPTY_ID;
	public String taskTitle = Task.EMPTY_STRING;
	private Task deletedTask = new Task();
	
	public ExecutionReply execute()
			throws TaskNotFoundException, NotUniqueException {
		assert(this.taskId != Task.EMPTY_ID ^ this.taskTitle != Task.EMPTY_STRING);
		SingleTaskReply r = new SingleTaskReply();
		int index = -1;
		if (this.taskId != Task.EMPTY_ID) {
			Task deleteTask = new Task();
			deleteTask.id = this.taskId;
			index = Logic.loadedData.indexOf(deleteTask);
		} else if (this.taskTitle != Task.EMPTY_STRING) {
			index = Logic.findIndexByTitle(this.taskTitle);
		}
		
		try {
			this.deletedTask = Logic.loadedData.remove(index);
			this.taskId = deletedTask.id;
			r.task = this.deletedTask;
		} catch (IndexOutOfBoundsException e) {
			throw new TaskNotFoundException();
		}
		
		Logic.saveData();
		Logic.updateCommandHistory(this);
		return r;
	}
	
	public void undo() {
		AddCommand add = new AddCommand();
		add.task = this.deletedTask;
		add.redo();
	}
	
	public void redo() {
		assert(taskId != Task.EMPTY_ID);
		Task deleteTask = new Task();
		deleteTask.id = this.taskId;
		int index = Logic.loadedData.indexOf(deleteTask);
		Logic.loadedData.remove(index);
		Logic.saveData();
	}
}
